package com.prs.crm.service.project.impl;

import java.util.Collection;

import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;

import com.prs.crm.context.Constrains;
import com.prs.crm.context.ToolKits;
import com.prs.crm.dao.project.TaskPerusalDao;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.project.TaskPerusal;
import com.prs.crm.service.hr.PersonService;
import com.prs.crm.service.impl.SimpleFlowServiceImpl;
import com.prs.crm.service.project.TaskPerusalService;

public class TaskPerusalServiceImpl extends SimpleFlowServiceImpl<TaskPerusal,TaskPerusalDao> implements TaskPerusalService{
	
	@Autowired
	private PersonService personService;
	
	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}


	public PersonService getPersonService() {
		return personService;
	}
	
	public void createTaskPerusal(TaskPerusal perusal,String recivers,boolean audit)
	{
		if (perusal==null)
			perusal = new TaskPerusal();
		Collection<Person> joiners = this.getPersonService().findPersons(ToolKits.convertStringAryToIntAry(recivers.split(",")));
		perusal.addAllReciver(joiners);
		if (audit)
		{
			perusal.setStatus(Constrains.TASKTRANSMIT_STATE_INIT);
			this.startFlow(perusal);
		}
		else
		{
			perusal.setStatus(Constrains.TASKTRANSMIT_STATE_VALID);
			perusal.getTask().addAllPerusals(perusal.getRecivers());
		}
		super.save(perusal);
	}
	

	@Override
	public void validateSubmit(TaskPerusal entity)
	{
		
	}
	
	public void startFlow(TaskPerusal entity)
	{
		this.save(entity);
		if (entity.getTask().getParent()!=null)
			entity.setVerifer(entity.getTask().getParent().getExecutor());
		else
			entity.setVerifer(entity.getTask().getProject().getRecorder());

		super.startFlow(entity);
	}
	
	@Override
	public void onFlowEnd(TaskPerusal entity)
	{
		/**
		 * 如果通过了审核，则任务执行状态为完成
		 */
		if (entity.getFlowstatus()==Constrains.WORKFLOWENTITY_STATE_VALID)
		{
			entity.setStatus(Constrains.TASKTRANSMIT_STATE_VALID);
			 entity.getTask().addAllPerusals(entity.getRecivers());
		}
		else
		{
			entity.setStatus(Constrains.TASKTRANSMIT_STATE_INVALID);
		}
		super.save(entity);
	}
	
	public PaginationSupport listVerifyPersualHistory(PaginationSupport page,Person recorder)
	{
		DetachedCriteria criteria =  this.createQuery(TaskPerusal.class);
		criteria.add(Restrictions.eq("recorder", recorder))
				.add(Restrictions.ne("status",Constrains.TASKPERUSAL_STATE_INIT));
		page.setOrders(new Order[]{Order.asc("id")});
		return super.listByPage(criteria, page);
	}


	public PaginationSupport listPerusalTask(PaginationSupport page,Person joiner)
	{
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(TaskPerusal.class);
		criteria.createAlias("recivers", "j").add(Restrictions.eq("j.id", joiner.getId()));
		page.setOrders(new Order[]{Order.asc("id")});
		return super.listByPage(criteria, page);
	}

}
